var session = sessionStorage.getItem("session");
if (!session) {
	window.parent.location = "../../login.html";
}
(function($, h, c) {
	var a = $([]),
		e = $.resize = $.extend($.resize, {}),
		i, k = "setTimeout",
		j = "resize",
		d = j + "-special-event",
		b = "delay",
		f = "throttleWindow";
	e[b] = 250;
	e[f] = true;
	$.event.special[j] = {
		setup: function() {
			if (!e[f] && this[k]) {
				return false
			}
			var l = $(this);
			a = a.add(l);
			$.data(this, d, {
				w: l.width(),
				h: l.height()
			});
			if (a.length === 1) {
				g()
			}
		},
		teardown: function() {
			if (!e[f] && this[k]) {
				return false
			}
			var l = $(this);
			a = a.not(l);
			l.removeData(d);
			if (!a.length) {
				clearTimeout(i)
			}
		},
		add: function(l) {
			if (!e[f] && this[k]) {
				return false
			}
			var n;

			function m(s, o, p) {
				var q = $(this),
					r = $.data(this, d);
				r.w = o !== c ? o : q.width();
				r.h = p !== c ? p : q.height();
				n.apply(this, arguments)
			}
			if ($.isFunction(l)) {
				n = l;
				return m
			} else {
				n = l.handler;
				l.handler = m
			}
		}
	};

	function g() {
		i = h[k](function() {
			a.each(function() {
				var n = $(this),
					m = n.width(),
					l = n.height(),
					o = $.data(this, d);
				if (m !== o.w || l !== o.h) {
					n.trigger(j, [o.w = m, o.h = l])
				}
			});
			g()
		}, e[b])
	}
})(jQuery, this);

var initT; //类型初始选中

$(document).ready(function() {
	$("#jqGrid").jqGrid({
		url: IP + 'GetProbeTaskMainList?callback=?&session=' + session,
		mtype: "Get",
		datatype: "jsonp",
		colModel: [{
			label: '序号',
			sorttype: 'integer',
			name: 'id',
			key: true,
			hidden: true,
			width: 50
		}, {
			label: '参数ID',
			name: 'paramArrayId',
			width: 60,
			hidden: true
		}, {
			label: '探针信息',
			name: 'probeInfo',
			width: 60,
		}, {
			label: '执行方式',
			name: 'executeType',
			editoptions: {
				value: "1:立即执行;2:间隔执行;3:永久执行"
			},
			formatter: 'select',
			editable: true,
			width: 60
		}, {
			label: '开始时间',
			name: 'sDate',
			width: 60
		}, {
			label: '结束时间',
			name: 'eDate',
			width: 60
		}, {
			label: '测试次数',
			name: 'cycleIndex',
			width: 60
		}, {
			label: '时间间隔',
			name: 'timeInterval',
			width: 60
		}, {
			label: '模板ID',
			name: 'taskModelId',
			width: 60,
			hidden: true,
		}, {
			label: '模板名称',
			name: 'taskModelName',
			width: 60
		}, {
			label: '质量评估',
			name: 'zlpgFalg',
			editoptions: {
				value: "0:否;1:是"
			},
			formatter: 'select',
			editable: true,
			width: 60
		}, {
			label: '警告标志',
			name: 'warningFlag',
			editoptions: {
				value: "0:否;1:是"
			},
			formatter: 'select',
			editable: true,
			width: 60
		}, {
			label: '告警次数',
			name: 'wTimes',
			width: 60
		}, {
			label: '成功率一般告警下限',
			name: 'cGeneralLow',
			width: 60
		}, {
			label: '成功率一般告警上限',
			name: 'cGeneralHigh',
			width: 60
		}, {
			label: '成功率严重告警下限',
			name: 'cSevereLow',
			width: 60
		}, {
			label: '成功率严重告警上限',
			name: 'cSevereHigh',
			width: 60
		}, {
			label: '延时一般告警下限',
			name: 'yGeneralLow',
			width: 60
		}, {
			label: '延时一般告警上限',
			name: 'yGeneralHigh',
			width: 60
		}, {
			label: '延时严重告警下限',
			name: 'ySevereLow',
			width: 60,
			hidden: true,
		}, {
			label: '延时严重告警上限',
			name: 'ySevereHigh',
			width: 60,

		}, {
			label: '测试类型',
			name: 'taskType',
			width: 60
		}, {
			label: '任务状态',
			name: 'taskState',
			editoptions: {
				value: "1:正在执行;2:任务挂起;3:任务结束;4:任务删除;"
			},
			formatter: 'select',
			editable: true,
			width: 60,
		}, {
			label: '探针标记',
			name: 'flag',
			width: 60,
			hidden: true,
		}, {
			label: '测试模型',
			name: 'flag2',
			width: 60,
			hidden: true,
		}, {
			label: '参数id',
			name: 'paramArrayId',
			width: 60,
			hidden: true,
		}],
		onSelectRow: function(rowid) {
			$("#taskSend").linkbutton("disable");
			$("#taskEdit").linkbutton("enable");
			$("#taskDelete").linkbutton("enable");
			$("#taskSuspeed").linkbutton("enable");
			$("#taskRetrun").linkbutton("enable");
		},
		autowidth: true,
		loadonce: true,
		viewrecords: true,
		rowNum: 15,
		page: 1,
		subGrid: true, // set the subGrid property to true to show expand buttons for each row
		subGridRowExpanded: showChildGrid, // javascript function that will take care of showing the child grid
		loadtext: '正在加载数据',
		pager: "#jqGridPager",
	});
	var h =$("body").height();
	$("#jqGrid").jqGrid("setGridHeight",h-400)

	//初始化列表；
	getMoOptions();
	var timer
	$("#pSearch").on("keyup", function() {
		var self = this;
		if (timer) {
			clearTimeout(timer);
		}
		timer = setTimeout(function() {
			//timer = null;
			$("#mGrid").jqGrid('filterInput', self.value);
		}, 0);
	});

	var timer1;
	$("#tSearch").on("keyup", function() {
		var self = this;
		if (timer1) {
			clearTimeout(timer1);
		}
		timer1 = setTimeout(function() {
			$("#jqGrid").jqGrid('filterInput', self.value);
		}, 0);
	})

	$("#taskContainer").resize(function() {
		var h = $(this).height();
		$("#jqGrid").jqGrid('setGridHeight', h - 95);
	})
	$("#cycleIndex").numberspinner({
		value: "72",
		onChange: function() {
			var cycle = $("#cycleIndex").numberspinner("getValue");
			if (cycle == 1) {
				$("#timeInterval").numberspinner("disable");
				$('#timeInterval').numberspinner('setValue', 1);
			} else {
				$("#timeInterval").numberspinner("enable");
				var s = $("#sDate").datetimebox("getValue");
				var e = $("#eDate").datetimebox("getValue");
				var sd = new Date(s);
				var ed = new Date(e);
				var times = (ed.getTime() - sd.getTime()) / cycle / 60000;
				$('#timeInterval').numberspinner('setValue', times);
			}
		}
	})
	$("#timeInterval").numberspinner({
		value: "60",
		onChange: function() {
			var times = $("#timeInterval").numberspinner("getValue");
			var s = $("#sDate").datetimebox("getValue");
			var e = $("#eDate").datetimebox("getValue");
			var sd = new Date(s);
			var ed = new Date(e);
			var cycle = (ed.getTime() - sd.getTime()) / times / 60000;
			$('#cycleIndex').numberspinner('setValue', cycle);
		}
	})
	$(":radio[name='execute']").click(function() {
		executeInit();
		var val = this.value;
		if (val == 1) {
			$("#s .easyui-datetimebox").datetimebox({
				readonly: true,
			});
			var s = getTime(0);
			$("#sDate").datetimebox("setValue", s);
			$("#timeInterval").numberbox("enable");
		} else if (val == 3) {
			$("#e .easyui-datetimebox").datetimebox({
				readonly: true,
			})
			$("#eDate").datetimebox("setValue", '2999-12-30 00:00:00');
			$("#cycleIndex").numberbox("disable");
			$("#cycleIndex").numberbox('setValue', 0);
		}
	})

});

//执行列表初始化
function executeInit() {
	$("#s .easyui-datetimebox").datetimebox({
		readonly: false
	});
	$("#e .easyui-datetimebox").datetimebox({
		readonly: false,
	})
	$("#cycleIndex").numberspinner("enable");
	$("#timeInterval").numberspinner("enable");
	var sTime = getTime(0);
	var eTime = getTime(3);
	$("#sDate").datetimebox("setValue", sTime);
	$("#eDate").datetimebox("setValue", eTime);
	$('#timeInterval').numberspinner('setValue', 60);
	$('#cycleIndex').numberspinner('setValue', 72);
}
//告警列表初始化
function warnInit() {
	$("#wTimes").numberbox("setValue", 10);

	$("#cGeneralLow").numberbox("setValue", 30);
	$("#cGeneralHigh").numberbox("setValue", 50);
	$("#cSevereLow").numberbox("setValue", 40);
	$("#cSevereHigh").numberbox("setValue", 60);

	$("#yGeneralLow").numberbox("setValue", 60);
	$("#yGeneralHigh").numberbox("setValue", 80);
	$("#ySevereLow").numberbox("setValue", 70);
	$("#ySevereHigh").numberbox("setValue", 90);

}

function getTime(t) {
	var curr_time = new Date();
	var strDate = curr_time.getFullYear() + "-";
	strDate += curr_time.getMonth() + 1 + "-";
	strDate += parseInt(curr_time.getDate()) + t + " ";
	strDate += curr_time.getHours() + ":";
	strDate += curr_time.getMinutes() + ":";
	strDate += curr_time.getSeconds();
	return strDate;
}

function mainInit() {

	$("#taskSend").linkbutton("enable");
	$("#taskEdit").linkbutton("disable");
	$("#taskDelete").linkbutton("disable");
	$("#taskSuspeed").linkbutton("disable");
	$("#taskRetrun").linkbutton("disable");

	$("#taskId").val("0");
	$("#probeDetail").val("");
	executeInit();
	warnInit();
	$("#zlpg").removeAttr("checked");
	$("#qyjg").removeAttr("checked");
	$("#modelType").combobox("setValue", "init");
	$("#paramGroup").combobox("setValue", "init");
	$("#testType").combobox("setValue", initT);
	var $table = $("<table id='mGrid'><table><div id='mGridPager'></div>");
	$("#myBox").empty().html($table);
	PingInit(null);
}

//加载模板下拉框
function getMoOptions() {
	$.ajax({
		url: IP + "GetMilParamGroupInfo?callback=?&session" + session,
		Type: "GET",
		dataType: 'json',
		jsonp: "callback",
		jsonpCallback: "resultHandler",
		success: function(data) {
			var s = {
				"id": "init",
				"modelMane": "--按组选择--"
			}
			data.unshift(s)
			$("#modelType").combobox({
				data: data,
				valueField: "id",
				textField: "modelMane",
				editable: false,
				onSelect: function() {
					changeModelType();
				}
			})
			getZuOptions();
		}
	})
}
//加载组下拉框
function getZuOptions() {
	$.ajax({
		url: IP + "GetSigleParamGroupInfo?callback=?&session" + session,
		Type: "GET",
		dataType: 'jsonp',
		jsonp: "callback",
		jsonpCallback: "resultHandler",
		success: function(data) {
			var s = {
				"id": "init",
				"paramGroup": "--按参数选择--"
			}
			data.unshift(s)
			$("#paramGroup").combobox({
				data: data,
				valueField: "id",
				textField: "paramGroup",
				editable: false,
				onSelect: function() {
					var ID = $("#paramGroup").combobox("getValue");
					var type;
					for (var i = 0; i < data.length; i++) {
						for (k in data[i]) {
							if (ID == data[i][k]) {
								type = data[i].type;
							}
						}
					}
					changeGroup(ID, type);
				}

			})
			getTestType()
		}
	})
}
//加载测试参数下拉框
function getTestType() {
	$.ajax({
		url: IP + "GetSystemParam?&callback=?&flag=1001" + "&session" + session,
		type: "Get",
		dataType: "jsonp",
		jsonp: "callback",
		jsonpCallback: "resultHandler",
		success: function(data) {
			//console.log(data);
			var options = data.vValue.split(",");
			initT = options[0];
			var sel = [];
			for (var i = 0; i < options.length; i++) {
				var n = [];
				n.push(options[i]);
				switch (options[i]) {
					case "Ping":
						n.push("Ping测试");
						break;
					case "TcpPing":
						n.push("TcpPing测试");
						break;
					case "NsLookUp":
						n.push("DNS测试");
						break;
					case "TraceRoute":
						n.push("路由追踪");
						break;

					case "FtpDownload":
						n.push("FTP下载");
						break;
					case "FtpUpload":
						n.push("FTP上传");
						break;
					case "WebDownload":
						n.push("网页浏览");
						break;
					case "HttpDownload":
						n.push('网页下载');
						break;
					case "RetrieveEmail":
						n.push("邮件接收");
						break;
					case "SendEmail":
						n.push("邮件发送");
						break;
					case "视频测试":
					    n.push("视频测试");
					    break;
				};
				sel.push(n);
			}
			$("#testType").combobox({
				data: sel,
				valueField: 0,
				textField: 1,
				editable: false,
				onSelect: function() {
					changeType()
				}

			})
			mainInit()
		}
	});

}
// the event handler on expanding parent row receives two parameters
// the ID of the grid tow  and the primary key of the row

function showChildGrid(parentRowID, parentRowKey) {
	var childGridID = parentRowID + "_table";
	var childGridPagerID = parentRowID + "_pager";
	// send the parent row primary key to the server so that we know which grid to show
	var childGridURL = IP + "GetProbeTaskDetailList?callback=?&id=" + parentRowKey;
	//childGridURL = childGridURL + "&parentRowID=" + encodeURIComponent(parentRowKey)
	// add a table and pager HTML elements to the parent grid row - we will render the child grid here
	$('#' + parentRowID).append('<table id=' + childGridID + '></table><div id=' + childGridPagerID + ' class=scroll></div>');
	$("#" + childGridID).jqGrid({
		url: childGridURL,
		mtype: "Get",
		datatype: "jsonp",
		page: 1,
		colModel: [{
			label: '创建時間',
			name: 'cTime',
			width: 100,

		}, {
			label: '测试地址',
			name: 'destination',
			width: 100,

		}, {
			label: '设备唯一编码',
			name: 'deviceid',
			width: 100
		}, {
			label: '模板名称',
			name: 'groupName',
			width: 70
		}, {
			label: '开始时间',
			name: 'sDate',
			width: 150,

		}, {
			label: '结束时间',
			name: 'eDate',
			width: 150
		}, {
			label: '间隔时间',
			name: 'meantime',
			width: 60
		}, {
			label: '执行次数',
			name: 'executeTimes',
			width: 70,

		}, {
			label: "任务名称",
			name: 'taskName',
			width: 260
		}, {
			label: '测试类型',
			name: 'taskType',
			width: 80
		}],
		loadonce: true,
		autowidth: true,
		height: 300,
		pager: "#" + childGridPagerID
	});

}